package com.icim.whtm.exporter.controller;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.icim.core.service.IAlarmSenderService;
import com.icim.csot.exporter.tx.csotshipping.CsotShippingO;
import com.icim.util.XmlConvertUtil;
import com.icim.whtm.exporter.service.IWhtmExportShippingDataService;

@Controller
public class WhtmShippingdataController {

	 @Resource
	    private IWhtmExportShippingDataService whtmExportShippingDataService;
	    
	    @Resource
	    private IAlarmSenderService alarmSenderService;
	    
	    private Logger logger = LoggerFactory.getLogger(WhtmShippingdataController.class);

	    @ResponseBody
	    @RequestMapping("exportWhtmShippingdata.do")
	    public String exportShippingData(String palletId,String ppboxId,String flag){

	        CsotShippingO outTrx = new CsotShippingO();
	       
	        try {
	        	logger.info("new project ...");
	        	//标记,timekey和boxname作为联合主键，回传做insert操作
	            outTrx = whtmExportShippingDataService.sendShippingData(palletId,ppboxId,flag);
	            //System.out.println(palletId+ppboxId+flag+"=====controller");
	            outTrx.setRtn_code("0000000");
	            outTrx.setRtn_mesg("SUCCESS");
	        } catch (Exception ex) {
	        	String errorMsg ="";
	        	if(flag.equals("LS"))
	        	{
	        		errorMsg="箱号："+ppboxId+"  数据回传失败\n"+"失败原因："+ex.getMessage();
	        	}
	        	else if(flag.equals("CH"))
	        	{
	        		errorMsg="栈板："+palletId+"  数据回传失败\n"+"失败原因："+ex.getMessage();
	        	}
	        	alarmSenderService.sendAlarm1("169",errorMsg);
	            outTrx.setRtn_code("9999999");
	            outTrx.setRtn_mesg(ex.getMessage());
	           logger.info("error: {} " ,ex);
	        }
	        return XmlConvertUtil.obj2Xml(outTrx, "transaction");
	    }
}
